package com.css.apps.base.user.tree;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.apps.base.common.provider.OrgQueryProvider;
import com.css.apps.base.common.provider.ReleateQueryProvider;
import com.css.apps.base.org.model.SOrg;
import com.css.apps.base.user.action.UserAction;
import com.css.apps.base.user.model.SUser;
import com.css.core.configuration.Environment;
import com.css.util.Ajax;
import com.css.util.ListUtil;
import com.css.util.Messages;
import com.css.util.StringHelper;

@SuppressWarnings("serial")
public class GetHeaderQuearterUserTree extends UserAction {

	private String userIds;
	
	@Override
	protected String userGo() {
		JSONArray res = new JSONArray();
		List<String> selectedIds = StringHelper.strToList(userIds);
		List<SOrg> allOrgList = OrgQueryProvider.listAllOrg();
		JSONObject o = null, u = null;
		Map<String, List<SUser>> deptUsers = ReleateQueryProvider.groupUsersByDeptId();
		for (SOrg org : allOrgList) {
			o = new JSONObject();
			String deptId = org.getUuid();
			o.put("id", deptId);
			o.put("name", org.getName());
			o.put("pId", org.getParentId());
			o.put("open", true);
			res.add(o);
			List<SUser> users = deptUsers.get(deptId);
			for (SUser user : users) {
				u = new JSONObject();
				u.put("id", user.getUserId());
				u.put("name", user.getRealName());
				u.put("pId", deptId);
				if (ListUtil.isLstNotEmpty(selectedIds) && selectedIds.contains(deptId)) {
					u.put("selected", true);
				}
				res.add(u);
			}
		}
		result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, Messages.getString("systemMsg.success"), res);
		return null;
	}

	public String getUserIds() {
		return userIds;
	}

	public void setUserIds(String userIds) {
		this.userIds = userIds;
	}

}
